package view;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import model.AppModel;
import view.panel.MenuPanel;
import view.panel.WordPanel;

public class AppView extends JFrame {

	private static final long serialVersionUID = 9060949726346588798L;
	
	private WordPanel jWord;
	private JPanel wrapper;
	private MenuPanel jMenu;
	
	public AppView(AppModel model) {
		
		this.setTitle("Flashcards");
		this.setPreferredSize(new Dimension(700, 200));
		this.setLocation(0, 0);
	//	this.setResizable(false);

		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowListener()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}

			public void windowOpened(WindowEvent e){}
			public void windowClosed(WindowEvent e){}
			public void windowIconified(WindowEvent e){}
			public void windowDeiconified(WindowEvent e){}
			public void windowActivated(WindowEvent e){}
			public void windowDeactivated(WindowEvent e){}
		});

		jMenu = new MenuPanel(this);
		jWord = new WordPanel(this, model);

		wrapper = new JPanel();
		this.add(wrapper);
		this.update(model);
		
		this.pack();
		this.setVisible(true);
	}
	
	public void update(AppModel model)
	{
		wrapper.removeAll();
		if(model.isStarted())
		{
			wrapper.add(jWord);
			jWord.update(model);
		}
		else
		{
			wrapper.add(jMenu);
			jMenu.update(model);
		}
		wrapper.updateUI();
	}
}